package cn.jy.nvr.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.jy.nvr.domain.APHERESES;
import cn.jy.nvr.domain.CommunRecorder;
import cn.jy.nvr.domain.PERIODIC;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.XY_BASE_COMMUNRECORD;
import cn.jy.nvr.mapper.XY_BASE_COMMUNRECORDMapper;
import cn.jy.nvr.service.ICommunRecorderService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class CommunRecordController {
	
	@Autowired
    ICommunRecorderService communRecorderService;	
	
	@Autowired
	XY_BASE_COMMUNRECORDMapper xy_base_communrecordmapper; 
	@RequestMapping("display/communRecordList")
	public String communRecordList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 response.setContentType("text/html");
		 response.setCharacterEncoding("utf-8");
		 String callback = request.getParameter("callback"); 
		 
		 String nurse = request.getParameter("nurse");
		 List<CommunRecorder> loadByNuser=new  ArrayList<CommunRecorder>();
		 if (!nurse.isEmpty()) {
			 CommunRecorder communRecorder=new CommunRecorder();
			 communRecorder.setNurse(nurse);
			 loadByNuser=communRecorderService.getAllByNurse(communRecorder);
			 
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = null;
		try {
			json = mapper.writeValueAsString(loadByNuser);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}

		String retStr = callback + "(" + json + ")";
		try {
			PrintWriter out = response.getWriter();
			out.print(retStr);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	} 
	
	@RequestMapping("display/communRecordDetail")
	public String communRecordDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 response.setContentType("text/html");
		 response.setCharacterEncoding("utf-8");
		 String callback = request.getParameter("callback"); 
		 
		 String id = request.getParameter("id");
		 CommunRecorder loadByNuser=null;
		 if (!id.isEmpty()) {
			 CommunRecorder communRecorder=new CommunRecorder();
			 communRecorder.setID(id);
			 loadByNuser=communRecorderService.getByNurseAndProvider(communRecorder);
			 
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = null;
		try {
			json = mapper.writeValueAsString(loadByNuser);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}

		String retStr = callback + "(" + json + ")";
		try {
			PrintWriter out = response.getWriter();
			out.print(retStr);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	} 

@RequestMapping("display/communRecordUpdate")
public String communRecordUpdate(HttpServletRequest request,HttpServletResponse response ,CommunRecorder communRecorder) throws Exception{
	 response.setContentType("text/html");
	 response.setCharacterEncoding("utf-8");
	 String callback = request.getParameter("callback"); 
	 String json = null;
	 communRecorder.setName(new String(communRecorder.getName().getBytes("ISO-8859-1"),"UTF-8"));
	
	 if (communRecorder!=null) {
		try {
			communRecorderService.update(communRecorder);
			json="修改成功";
		} catch (Exception e) {
			e.printStackTrace();
			json="修改失败";
		}
	}else {
		json="修改失败";
	}
	 XY_BASE_COMMUNRECORD xy_base_communrecord=new XY_BASE_COMMUNRECORD();
	 xy_base_communrecord.setHSXM(communRecorder.getNurse());
	List<XY_BASE_COMMUNRECORD> loadByNuser = xy_base_communrecordmapper.loadByNuser(xy_base_communrecord);
	int size = loadByNuser.size();
	  json=json+"还剩"+String.valueOf(size)+"条未处理";
	 ObjectMapper mapper = new ObjectMapper();
	 
	 try {
			json = mapper.writeValueAsString(json);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}
	String retStr = callback + "(" + json + ")";
	try {
		PrintWriter out = response.getWriter();
		out.print(retStr);
		out.flush();
		out.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return null;
} 
}